// <copyright file="Logger.cs" company="YUIAsp.Net">
// Copyright (c) 2009 All Right Reserved
// </copyright>
// <author>raul popescu</author>
// <email>dev@yuiasp.net</email>
// <date>2009-01-30</date>
// <summary>YUIAsp.Net File</summary>

using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;
using YUIAspNet.Common;

#region Embedded resources definitions
[assembly: WebResource("YUIAspNet.Logger.Logger.js", "text/javascript")]
[assembly: WebResource("YUIAspNet.EmbeddedSkins.DefaultSkin.Logger.logger-skin.css", "text/css", PerformSubstitution = true)]
#endregion

namespace YUIAspNet
{
    /// <summary>
    /// Logger control.
    /// </summary>
    [ToolboxItem(true)]
    [ParseChildren(true)]
    public class Logger : YUIScriptBaseControl, INamingContainer
    {
        Panel _logContainer;

        /// <summary>
        /// Initializes a new instance of the <see cref="Logger"/> class.
        /// </summary>
        public Logger()
        {
            Init += new EventHandler(Logger_Init);
            PreRender += new EventHandler(Logger_PreRender);

            //default values
            Draggable = true;
            FooterEnabled = true;
            MaxDisplayMessageCount = 500;
            MinDisplayMessageCount = 100; 
            Verbose = true;
        }

        void Logger_Init(object sender, EventArgs e)
        {
            _logContainer = new Panel();
            _logContainer.ID = "log";
            Controls.Add(_logContainer);
        }

        void Logger_PreRender(object sender, EventArgs e)
        {
            _logContainer.Width = Width;
            _logContainer.Height = Height;

            if (!string.IsNullOrEmpty(Top))
                _logContainer.Style.Add("top", Top);
            if (!string.IsNullOrEmpty(Bottom))
                _logContainer.Style.Add("bottom", Bottom);
            if (!string.IsNullOrEmpty(Left))
                _logContainer.Style.Add("left", Left);
            if (!string.IsNullOrEmpty(Right))
                _logContainer.Style.Add("right", Right);

            if(_logContainer.Style.Count > 0)
                _logContainer.Style.Add("position", "absolute");
        }

        /// <summary>
        /// Registers the style sheets for the current control.
        /// Must be overriden in the parent control to add any css file reference.
        /// </summary>
        protected override void RegisterStyleSheets()
        {
            //todo: skin file used...
            RegisterEmbeddedStylesheet("Logger", "logger-skin.css");
        }

        /// <summary>
        /// Register the values to send to the client control for initialization.
        /// </summary>
        protected override void RegisterClientInitialization() 
        {
            AddProperty("LogContainer", _logContainer.ClientID);
            
            AddProperty("FooterEnabled", FooterEnabled);
            AddProperty("Draggable", Draggable);
            AddProperty("IsCollapsed", IsCollapsed);
            AddProperty("IsPaused", IsPaused);
            AddProperty("NewestOnTop", NewestOnTop);

            AddProperty("MaxDisplayMessageCount", MaxDisplayMessageCount);
            AddProperty("MinDisplayMessageCount", MinDisplayMessageCount);

            AddProperty("Verbose", Verbose);
        }

        /// <summary>
        /// Registers any additional scripts to include for this control.
        /// </summary>
        protected override void RegisterScriptReferences()
        {
            AddScriptReference(new ScriptReference("YUIAspNet.Logger.Logger.js", this.GetType().Assembly.FullName));
        }

        #region Public properties
        /// <summary>
        /// Gets or sets the top alignment.
        /// </summary>
        /// <value>The top.</value>
        public string Top { get; set; }

        /// <summary>
        /// Gets or sets the left alignment.
        /// </summary>
        /// <value>The left.</value>
        public string Left { get; set; }

        /// <summary>
        /// Gets or sets the right alignment.
        /// </summary>
        /// <value>The right.</value>
        public string Right { get; set; }

        /// <summary>
        /// Gets or sets the bottom alignment.
        /// </summary>
        /// <value>The bottom.</value>
        public string Bottom { get; set; }

        /// <summary>
        /// Gets or sets a value indicating whether this <see cref="Logger"/> is draggable.
        /// </summary>
        /// <value><c>true</c> if draggable; otherwise, <c>false</c>.</value>
        public bool Draggable { get; set; }

        /// <summary>
        /// Gets or sets a value indicating whether the footer is enabled.
        /// </summary>
        /// <value><c>true</c> if footer should be enabled; otherwise, <c>false</c>.</value>
        public bool FooterEnabled { get; set; }

        /// <summary>
        /// Gets or sets a value indicating whether this instance is collapsed.
        /// </summary>
        /// <value>
        /// 	<c>true</c> if this instance is collapsed; otherwise, <c>false</c>.
        /// </value>
        public bool IsCollapsed { get; set; }

        /// <summary>
        /// Gets or sets a value indicating whether this instance is paused.
        /// </summary>
        /// <value><c>true</c> if this instance is paused; otherwise, <c>false</c>.</value>
        public bool IsPaused { get; set; }

        /// <summary>
        /// Gets or sets a value indicating whether newest logger should be displaied on top.
        /// </summary>
        /// <value><c>true</c> to display the newest on top; otherwise, <c>false</c>.</value>
        public bool NewestOnTop { get; set; }

        /// <summary>
        /// Gets or sets the max display message count.
        /// </summary>
        /// <value>The max display message count.</value>
        public int MaxDisplayMessageCount { get; set; }

        /// <summary>
        /// Gets or sets the min display message count.
        /// </summary>
        /// <value>The min display message count.</value>
        public int MinDisplayMessageCount { get; set; }

        /// <summary>
        /// Gets or sets a value indicating whether this <see cref="Logger"/> is verbose.
        /// </summary>
        /// <value><c>true</c> if verbose; otherwise, <c>false</c>.</value>
        public bool Verbose { get; set; } 
        #endregion
    }
}
